class LyricParser:

    """
    歌词解析类
    """

    def __init__(self, lrcs: str):
        """
        解析歌词
        :param lrcs:歌词内容
        """

        try:
            lyrics = lrcs.split('[')[1:]

            self.lrc_times = []
            self.lrc_texts = []
            for i in lyrics:
                origin_time = i.split(']')[0].split('.')[0]
                minute = int(origin_time.split(':')[0])
                second = int(origin_time.split(':')[1])
                millisecond = int(i.split(']')[0].split('.')[1])
                time = minute * 60 + second + (millisecond / 1000)
                self.lrc_times.append(time)
                self.lrc_texts.append(i.split(']')[1].strip('\n'))

            # for i in range(len(self.lrc_texts)):
            #     print(self.lrc_times[i], self.lrc_texts[i])

        except Exception as e:
            print("Lyric Module Error: ", e)

    def get_lyric(self, time: float):
        """
        获取当前时间的歌词
        :param time: 按照Audio控件返回值的格式传入，即秒数不会按60进位，以str格式传入
        :return: 列表，三项，显示的三句歌词
        """

        try:
            play_time = time
            li = ["", "", ""]
            flag = 0
            for i in range(len(self.lrc_times)):
                if play_time >= self.lrc_times[i]:
                    # print(play_time, self.lrc_times[i])
                    li.append(self.lrc_texts[i])
                    flag = i

            try:
                li.append(self.lrc_texts[flag + 1])
            except:
                li.append("")
            return li[-3:]
        except:
            return ["", "没有歌词", ""]


if __name__ == '__main__':
    lp = LyricParser("C:/try.lrc")
